package cn.kailangtu.commontools;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.util.Log;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class Loggers {

    private static Logger netLogger = null;
    private static Logger hardwareLogger = null;
    private static Logger errLogger = null;
    private static Logger testLogger = null;

    /**
     *
     *
     * getNetLogger(获取网络请求logger)
     *
     * @Title: getNetLogger
     * @Description: TODO
     * @param @return 设定文件
     * @return Logger 返回类型
     * @throws
     */
    public static synchronized Logger getNetLogger() {
        String modelName = "net";
        if (netLogger == null) {
            try {
                FileAppender fileAppender = createPath(modelName);
                netLogger = Logger.getLogger(modelName);
                netLogger.addAppender(fileAppender);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return netLogger;
    }

    public static void netInfo(String msg)
    {
       // getNetLogger().info(msg);
    }

    public static void errInfo(String msg){
        //getErrLogger().info(msg);
    }

    public static void testInfo(String msg){
        //getTestLogger().info(msg);
        Log.d("SHOOTLEETEST",msg);
    }

    public static void hardwareInfo(String msg){
        //getHardwareLogger().info(msg);
    }

    public static void serialPortApiInfo(String msg)
    {
        //getHardwareLogger().info(msg);
    }

    /**
     *
     *
     * getErrLogger(获取网络请求logger)
     *
     * @Title: getErrLogger
     * @Description: TODO
     * @param @return 设定文件
     * @return Logger 返回类型
     * @throws
     */
    public static synchronized Logger getErrLogger() {
        String modelName = "error";
        if (errLogger == null) {
            try {
                FileAppender fileAppender = createPath(modelName);
                errLogger = Logger.getLogger(modelName);
                errLogger.addAppender(fileAppender);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return errLogger;
    }

    public static synchronized  Logger getTestLogger(){
        String modelName="test";
        if(testLogger == null){
            try {
                FileAppender fileAppender = createPath(modelName);
                testLogger = Logger.getLogger(modelName);
                testLogger.addAppender(fileAppender);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return testLogger;
    }


    /**
     *
     *
     * getHardwareLogger(获取硬件模块logger)
     *
     * @Title: getHardwareLogger
     * @Description: TODO
     * @param @return 设定文件
     * @return Logger 返回类型
     * @throws
     */
    public static synchronized Logger getHardwareLogger() {
        String modelName = "hardware";
        if (hardwareLogger == null) {
            try {
                FileAppender fileAppender = createPath(modelName);
                hardwareLogger = Logger.getLogger(modelName);
                hardwareLogger.addAppender(fileAppender);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return hardwareLogger;
    }

    /**
     *
     *
     * createPath(根据模块名称生成log文件目录)
     *
     * @Title: creeatePath
     * @Description: TODO
     * @param @param modelName
     * @param @return
     * @param @throws IOException 设定文件
     * @return FileAppender 返回类型
     * @throws
     */
    private static FileAppender createPath(String modelName)
            throws IOException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "tenreg"
                + File.separator
                + "log"
                + File.separator
                + modelName
                + File.separator + sdf.format(date);
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} %m %n");
        String fileName = "log-" + modelName + "-" + sdf.format(date) + ".log";
        FileAppender fileAppender = new FileAppender(patternLayout, path
                + File.separator + fileName);
        return fileAppender;
    }

    public static void clearLog(String currentTime){

    }
}